home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 6 / 006.d81 / dos #14 < prev    next >
Text File  |  2022-08-26  |  4KB  |  217 lines

  1.       DOS & DON'TS -- Part 14
  2.       =======================
  3.  
  4.  
  5.  
  6.    Now for some Examples!  Let's ima-
  7.  
  8. gine a program that needs to keep tabs
  9.  
  10. on 100 strings and 100 numbers.  Since
  11.  
  12. (assuming the strings are small) this
  13.  
  14. can be kept in memory, the data will
  15.  
  16. be manipulated in the form of a string
  17.  
  18. array (say, S$) and a numeric array
  19.  
  20. (say, N).  But, the data must be kept
  21.  
  22. intact between program 'RUNs'.  Since
  23.  
  24. RAM is notorious for forgetting things
  25.  
  26. every time the power goes or someone
  27.  
  28. types 'NEW' or 'CLR' or runs another
  29.  
  30. program, we use a sequential file on
  31.  
  32. disk to hold the 'goods'.
  33.  
  34.  
  35.    Let's suppose that this data is in
  36.  
  37. a file called 'DATAFILE'.  We must
  38.  
  39. first OPEN the CEC and set up some
  40.  
  41. variables we will need:
  42.  
  43.  
  44.    10 OPEN 15,8,15,'I0': F$='DATAFILE'
  45.    : R$=CHR$(13): DIM S$(100), N(100)
  46.  
  47.  
  48. Now let's check to see if the file is
  49.  
  50. on the disk.  We will use the trick we
  51.  
  52. learned in a previous issue:
  53.  
  54.  
  55.    20 PRINT#15,'R0:';F$;'=';F$: INPUT
  56.    #15, ER%, ER$, ET%, ES%
  57.    30 IF ER%=62 THEN 1000: REM FILE
  58.    NOT FOUND
  59.    40 IF ER%<>63 THEN 2000: REM ERROR
  60.    IF NOT A 'FILE EXISTS' MESSAGE.
  61.  
  62.  
  63. If the file does not exist, we will
  64.  
  65. handle that in line 1000.  If a disk
  66.  
  67. error occurred, we will handle that in
  68.  
  69. line 2000.  If the file does exist,
  70.  
  71. however, we will OPEN it and read its
  72.  
  73. contents:
  74.  
  75.  
  76.    50 OPEN 2,8,2,F$: REM NO OPTIONS
  77.    NEEDED FOR SEQUENTIAL READ.
  78.    60 FOR I=1 TO 100: INPUT#2, S$(I);
  79.    N(I): NEXT:  CLOSE 2
  80.  
  81.  
  82. We have now 'loaded' the arrays with
  83.  
  84. the contents of the file 'DATAFILE'!
  85.  
  86.  
  87.    Later on in the program, we have
  88.  
  89. updated the data and want to write it
  90.  
  91. out to a new version of 'DATAFILE'.
  92.  
  93. Here is what we do:
  94.  
  95.  
  96.    500 PRINT#15, 'S0:'+F$: REM SCRATCH
  97.    OLD VERSION FIRST!
  98.    510 OPEN 2, 8, 2, F$ + ',S,W': REM
  99.    OPEN SEQUENTIAL FILE FOR WRITE.
  100.    520 FOR I=1 TO 100: PRINT #2, S$(I)
  101.    ; R$; N(I): NEXT I: CLOSE 2
  102.  
  103.  
  104.    Notice the use of the separator
  105.  
  106. variable R$ (which was assigned a car-
  107.  
  108. riage return character in line 10) in
  109.  
  110. line 520.
  111.  
  112.  
  113.    The routine at line 1000 which han-
  114.  
  115. dles the case of the 'DATAFILE' not
  116.  
  117. being on the disk might be like this:
  118.  
  119.  
  120.    1000 PRINT 'DATAFILE NOT FOUND!':
  121.    INPUT 'MAKE ONE FROM SCRATCH? ';A$
  122.    1010 IF LEFT$(A$,1)='Y' THEN 1100
  123.    1020 PRINT 'PUT IN CORRECT DISK':
  124.    PRINT 'THEN PRESS A KEY:'
  125.    1030 POKE 198, 0: WAIT 198, 1:
  126.    POKE 198, 0: GOTO 20: REM GET KEY.
  127.    1100 OPEN 2, 8, 2, F$+',S,W': REM
  128.    OPEN FOR SEQUENTIAL WRITE.
  129.    1110 FOR I=1 TO 100:  PRINT #2,
  130.    CHR$(160); R$; 0: NEXT I: CLOSE 2
  131.    1120 GOTO 20
  132.  
  133.  
  134.    Lines 1000-1030 ask the user if the
  135.  
  136. file needs to be created from scratch.
  137.  
  138. If not, line 1030 waits for a keypress
  139.  
  140. then goes back to line 20, which looks
  141.  
  142. for the file again.
  143.  
  144.  
  145.    Lines 1100-1120 create a new 'DATA-
  146.  
  147. FILE'.  In line 1110, a CHR$(160), or
  148.  
  149. 'shifted-space' is used for the empty
  150.  
  151. strings.  This is because BASIC skips
  152.  
  153. over any blank lines in a file!  In
  154.  
  155. all, 100 empty strings and zeros are
  156.  
  157. written to the file.
  158.  
  159.  
  160.    The routine at line 2000 handles
  161.  
  162. disk errors.  It checks the value of
  163.  
  164. ER% (obtained from the CEC in line 30)
  165.  
  166. and acts upon several possibilities:
  167.  
  168.  
  169.    2000 IF ER% <> 26 THEN 2100
  170.    2010 PRINT 'REMOVE THE DISK, ';
  171.    'TAKE OFF THE WRITE-PROTECT TAB,'
  172.    2020 PRINT 'THEN REPLACE THE ';
  173.    'DISK AND PRESS A KEY:'
  174.    2030 POKE 198, 0: WAIT 198, 1:
  175.    POKE 198, 0:  GOTO 20
  176.  
  177.    2100 IF ER% <> 74 THEN 2200
  178.    2110 PRINT 'CLOSE THE DISK';
  179.    'DOOR, THEN PRESS A KEY:'
  180.    2120 GOTO 2030
  181.  
  182.    2200 PRINT 'DISK ERROR #'; ER%;
  183.    'TYPE: '; ER$
  184.    2210 PRINT 'ON BLOCK '; ES%;
  185.    ' OF TRACK ';ET%: END
  186.  
  187.  
  188.    Lines 2000-2030 check for a Write
  189.  
  190. Protect tab (error #26).  Lines 2100-
  191.  
  192. 2120 handle a 'Drive Not Ready' error
  193.  
  194. (#74).  This is often caused by the
  195.  
  196. disk drive door being open, or no disk
  197.  
  198. being in the drive.  Lines 2200 & 2210
  199.  
  200. display other errors, which usually
  201.  
  202. mean a bug in the program or a bad
  203.  
  204. disk in the drive.
  205.  
  206.  
  207.    This concludes this part of our
  208.  
  209. study of Sequential Files.  Hope you
  210.  
  211. have maybe learned something.  Have
  212.  
  213. fun!!
  214.  
  215.  
  216. ============End of Article============
  217.